package com.wiz.web.controllor;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.entity.Resources;
import com.wiz.entity.Roles;
import com.wiz.service.ResourcesService;
import com.wiz.service.RolesService;
import com.wiz.util.PageView;
import com.wiz.vo.ResponseJsonBody;

/**
 * 
 * @author xxy
 *
 */
@Controller
@RequestMapping(value="/role")
public class RoleController {
	private transient final Logger log=LoggerFactory.getLogger(getClass());
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Autowired
	private RolesService rolesService;
	@Autowired
	private ResourcesService resourcesService;
	/**
	 * 跳到新增页面
	 * @return
	 */
	@RequestMapping(value="addUI")
	public String addUI(Model model){
		this.permissio(model);
		return "/role/add";
	} 
	/**
	 * 保存新增
	 * @param model
	 * @param role
	 * @return
	 */
	@RequestMapping(value="add")
	@ResponseBody
	public String add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "name", required = true) String name,//角色名
			@RequestParam(value = "roleKey", required = true) String roleKey,//roleKey
			@RequestParam(value = "enable", required = true) String enable,//是否禁用
			@RequestParam(value = "description", required = true) String description,//描述
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		try {
			Roles role = new Roles(null, Integer.valueOf(enable), name, roleKey, description, null);
			rolesService.add(role);
			
			json = new ResponseJsonBody("200", "保存成功", "sys_role",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_role",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
	}
	
	/**
	 * 分页查询
	 * @param model
	 * @param role
	 * @param pageNow
	 * @return
	 */
	@RequestMapping(value="query")
	public String query(Model model,Roles role,String pageNow){
		PageView pageView = null;
		if(StringUtils.isEmpty(pageNow)){
			pageView = new PageView(1);
		}else{
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		pageView = rolesService.query(pageView, role);
		model.addAttribute("pageView", pageView);
		return "/role/list";
	}
	/**
	 * 更新修改的信息
	 * @param model
	 * @param role
	 * @return
	 */
	@RequestMapping(value="update")
	@ResponseBody
	public String updaterole(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id,//角色名
			@RequestParam(value = "name", required = true) String name,//角色名
			@RequestParam(value = "roleKey", required = true) String roleKey,//roleKey
			@RequestParam(value = "enable", required = true) String enable,//是否禁用
			@RequestParam(value = "description", required = true) String description,//描述
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		Roles role = new Roles(Integer.valueOf(id), Integer.valueOf(enable), name, roleKey, description, null);
		try {
			rolesService.modify(role);
			
			json = new ResponseJsonBody("200", "保存成功", "sys_role",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_role",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
	}
	/**
	 * 权限树
	 * @return
	 */
	@RequestMapping(value="permissio")
	public String permissio(Model model){
		List<Resources> allRes = resourcesService.findAll();
		StringBuffer sb = new StringBuffer();
		sb.append("var data = [];");
		for (Resources r : allRes) {
				sb.append("data.push({ fid: '"
						+ r.getId() + "', pfid: '"
						+ r.getParentId()
						+ "', fname: '" + r.getName()
						+ "'});");

			}
		model.addAttribute("resources", sb);
		return "/resources/permissioUser";
	}
	
	
	/**
	 * 根据id删除
	 * @param model
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value="deleteById")
	@ResponseBody
	public String deleteById(Model model,String roleId){
		ResponseJsonBody json = null;
		try {
			rolesService.delete(roleId);
			json = new ResponseJsonBody("200", "修改成功", "sys_role",
					"", "", "", "");
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "sys_role",
					"", "", "", "");
		}
		return gson.toJson(json);
	}
	
	/**
	 * 查询单条记录
	 * @param model
	 * @param roleId
	 * @param type
	 * @return
	 */
	@RequestMapping(value="getById")
	public String getById(Model model,String roleId,int typeKey){
		Roles role = rolesService.getById(roleId);
		model.addAttribute("role", role);
		if(typeKey == 1){
			return "/role/edit";
		}else{
			return "/role/show";
		}
	}
	
	
}
